package com.ck.ips.schedule;

import com.ck.core.lock.LockConstant;
import com.ck.core.lock.RedisDistributeLock;
import com.ck.core.lock.RedisLock;
import com.ck.ips.dao.DataBoardMapper;
import com.ck.ips.model.vo.board.DeviceScreenVo;
import com.ck.ips.model.vo.board.MonitorDataVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 数据看板job
 *
 * @author XC17125
 */
@Component
public class DataBoardJob {

    public static final String BIG_SCREEN_SIZE = "75";
    private Logger logger = LoggerFactory.getLogger(DataBoardJob.class);

    @Autowired
    private RedisDistributeLock distributeLock;

    @Autowired
    private DataBoardMapper dataBoardMapper;

    /**
     * 数据看板Led屏幕状态更新每日数据
     */
    @Async
    @Scheduled(cron = "0 30 10 * * ?")
    public void statisticLedStatus() {
        logger.info(">>>  数据看板Led屏幕状态更新每日数据");

        // job加锁，避免在分布式环境多个应用同时执行任务
        try (RedisLock lock = distributeLock.getLock(LockConstant.JOB_TASK_EXECUTE_LOCK_PREFIX + this.getClass().getName() + "_statisticLedStatus",
                180L, 0, LockConstant.RETRY_INTERVAL_TIME_MILLIS)) {
            if (null != lock) {
                DeviceScreenVo bigScreen = dataBoardMapper.getBigScreenCount();
                DeviceScreenVo smallScreen = dataBoardMapper.getSmallScreenCount();
                //获取当前时间
                String currentDayTime = getCurrentDayTimeWithoutYear(System.currentTimeMillis());
                Long smallTotal = 0L;
                Long bigTotal = 0L;
                Long smallOnlineTotal = 0L;
                Long bigOnlineTotal = 0L;
                if (smallScreen != null) {
                    smallTotal = smallScreen.getDeviceCount();
                    smallOnlineTotal = smallScreen.getDeviceCount() - smallScreen.getOffLineCount();
                }
                if (bigScreen != null) {
                    bigTotal = bigScreen.getDeviceCount();
                    bigOnlineTotal = bigScreen.getDeviceCount() - bigScreen.getOffLineCount();
                }
                dataBoardMapper.insertScreenStatisticData(currentDayTime, smallTotal, smallOnlineTotal, bigTotal, bigOnlineTotal);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

    }

    /**
     * 每日刊播汇总统计
     * ck_monitor_data_file
     */
    @Async
    @Scheduled(cron = "0 30 5 * * ?")
    public void statisticMonitorData() {
        logger.info(">>>  每日刊播汇总统计");

        // job加锁，避免在分布式环境多个应用同时执行任务
        try (RedisLock lock = distributeLock.getLock(LockConstant.JOB_TASK_EXECUTE_LOCK_PREFIX + this.getClass().getName() + "_statisticMonitorData",
                180L, 0, LockConstant.RETRY_INTERVAL_TIME_MILLIS)) {
            if (null != lock) {
                //获取昨日的统计数据
                long lastDay = System.currentTimeMillis() - 24 * 60 * 60 * 1000;
                String playDate = getCurrentDayTimeStrWithYear(lastDay);
                String dayTime = getCurrentDayTimeWithoutYear(lastDay);
                MonitorDataVo data = dataBoardMapper.getMonitorDataByDate(playDate);
                if (data == null) {
                    return;
                }
                data.setDayTime(dayTime);
                Long playCount = data.getPlayCount();
                Long deviceCount = data.getDeviceCount();
                if (playCount == null) {
                    data.setPlayCount(0L);
                }
                if (deviceCount == null) {
                    data.setDeviceCount(0L);
                }
                dataBoardMapper.insertMonitorStatisticData(data);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

    }

    private String getCurrentDayTimeWithoutYear(long timeMillis) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd");
        Date date = new Date(timeMillis);
        String time = formatter.format(date);
        return time;
    }

    private String getCurrentDayTimeStrWithYear(long timeMillis) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date(timeMillis);
        String time = formatter.format(date);
        return time;
    }


}
